home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Interfaces / CIncludes / complex.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-28  |  10.4 KB  |  444 lines  |  [TEXT/MPS ]

  1. /*ident    "@(#)complex.h    1.3   6/2/90" */
  2.  
  3. #ifndef __COMPLEX__
  4. #define __COMPLEX__    1
  5.  
  6. /*
  7. **  (c) Copyright 1988-1994 by Dyad Software Corp.
  8. **  All Rights Reserved
  9. **
  10. **  Authors: lwd, geb
  11. */
  12.  
  13. #ifndef __MATH__
  14. #include <math.h>
  15. #endif
  16.  
  17. #ifndef __FLTPNT_H
  18. #include <fltpnt.h>
  19. #endif
  20.  
  21. #ifndef FLT_TYPE
  22. #define FLT_TYPE long double
  23. #endif
  24.  
  25. class ostream;
  26. class istream;
  27.  
  28.  
  29. class complex
  30. {
  31.  
  32.  public:
  33.    friend FLT_TYPE real ( const complex& );     
  34.    friend FLT_TYPE imag ( const complex& );     
  35.  
  36.    friend complex cos ( const complex& );
  37.    friend complex cosh ( const complex& );
  38.    friend complex sin ( const complex& );
  39.    friend complex sinh ( const complex& );
  40.    friend complex tan ( const complex& );
  41.    friend complex tanh ( const complex& );
  42.    friend complex log ( const complex& );
  43.    friend complex log10 ( const complex& );
  44.    friend complex sqrt ( const complex& );
  45.    friend FLT_TYPE abs ( const complex& );
  46.    friend complex conj ( const complex& );
  47.    friend FLT_TYPE norm ( const complex& );
  48.    friend FLT_TYPE modulus ( const complex& );
  49.    friend FLT_TYPE arg ( const complex& );
  50.    friend complex asin ( const complex& );
  51.    friend complex acos ( const complex& );
  52.    friend complex atan ( const complex& );
  53.    friend complex asinh ( const complex& );
  54.    friend complex atanh ( const complex& );
  55.    friend complex exp ( const complex& );
  56.    friend complex polar ( FLT_TYPE r, FLT_TYPE theta = 0);
  57.  
  58.    friend complex operator + ( const complex&, const complex& );
  59.    friend complex operator + ( FLT_TYPE, const complex& );
  60.    friend complex operator + ( const complex&, FLT_TYPE );
  61.  
  62.    friend complex operator - ( const complex&, const complex& );
  63.    friend complex operator - ( FLT_TYPE, const complex& );
  64.    friend complex operator - ( const complex&, FLT_TYPE );
  65.  
  66.    friend complex operator * ( const complex&, const complex& );
  67.    friend complex operator * ( FLT_TYPE, const complex& );
  68.    friend complex operator * ( const complex&, FLT_TYPE );
  69.  
  70.    friend complex operator / ( const complex&, const complex& );
  71.    friend complex operator / ( FLT_TYPE, const complex& );
  72.    friend complex operator / ( const complex&, FLT_TYPE );
  73.  
  74.    friend complex pow( const complex& lhs, FLT_TYPE rhs );
  75.    friend complex pow( const complex& lhs, const complex & rhs );
  76.    friend complex pow( FLT_TYPE lhs, const complex & rhs);
  77.    friend complex pow( const complex& lhs, int rhs);
  78.  
  79.    friend int operator && ( const complex&, const complex& );
  80.    friend int operator && ( FLT_TYPE, const complex& );
  81.    friend int operator && ( const complex&, FLT_TYPE );
  82.  
  83.    friend int operator || ( const complex&, const complex& );
  84.    friend int operator || ( FLT_TYPE, const complex& );
  85.    friend int operator || ( const complex&, FLT_TYPE );
  86.  
  87.    friend int operator != ( const complex&, const complex& );
  88.    friend int operator != ( FLT_TYPE, const complex& );
  89.    friend int operator != ( const complex&, FLT_TYPE );
  90.  
  91.    friend int operator == ( const complex&, const complex& );
  92.    friend int operator == ( FLT_TYPE, const complex& );
  93.    friend int operator == ( const complex&, FLT_TYPE );
  94.  
  95.    friend ostream& operator << ( ostream& s, const complex& x);
  96.    friend istream& operator >> ( istream& s, complex& x);
  97.  
  98.  public:
  99.  
  100.    complex ( );
  101.    complex ( FLT_TYPE r, FLT_TYPE i ); /*   this is not declared (FLT_TYPE r, FLT_TYPE i = 0)
  102.                                         to avoid implicit conversion. Binary operations
  103.                                         are explicitly declared thus avoiding potential 
  104.                                         ambiguities. */
  105.    complex ( const complex & );   
  106.    FLT_TYPE& real ( );
  107.    FLT_TYPE& imag ( );
  108.    
  109.    complex& operator = ( const complex& );
  110.    complex& operator = ( FLT_TYPE );
  111.    
  112.    complex& operator += ( const complex& );
  113.    complex& operator += ( FLT_TYPE );
  114.    
  115.    complex& operator -= ( const complex& );
  116.    complex& operator -= ( FLT_TYPE );
  117.    
  118.    complex& operator *= ( const complex& );
  119.    complex& operator *= ( FLT_TYPE );
  120.    
  121.    complex& operator /= ( const complex& );
  122.    complex& operator /= ( FLT_TYPE );
  123.    
  124.    int operator ! () const;
  125.    complex operator - () const;
  126.    
  127.  private:
  128.    FLT_TYPE re;
  129.    FLT_TYPE im;
  130.  };
  131.  
  132. inline complex::complex ( )
  133. {
  134. #if 0
  135.   /* remove these comments for initialization to (0,0)
  136.      re = im = 0; 
  137.      */
  138. #else
  139.     re = im = NAN;
  140. #endif
  141. }
  142.  
  143. inline complex::complex ( FLT_TYPE r, FLT_TYPE i ): re(r), im(i) {}
  144.      inline complex::complex( const complex& z)
  145. {
  146.   re = z.re;
  147.   im = z.im;
  148. }    
  149.  
  150. inline FLT_TYPE real ( const complex& z){ return z.re; }     
  151. inline FLT_TYPE imag ( const complex& z){ return z.im; }
  152.      
  153. inline FLT_TYPE& complex::real (){ return re; }     
  154. inline FLT_TYPE& complex::imag (){ return im; }
  155.      
  156. /* operator + */
  157. inline complex operator + ( const complex& lhs, const complex& rhs )
  158. {
  159.   return  complex ( lhs.re + rhs.re, lhs.im + rhs.im );
  160. }
  161. inline complex operator + ( FLT_TYPE  lhs, const complex& rhs )
  162. {
  163.   return  complex ( lhs + rhs.re, rhs.im );
  164. }
  165. inline complex operator + ( const complex& lhs, FLT_TYPE rhs )
  166. {
  167.   return  complex ( lhs.re + rhs, lhs.im );
  168. }
  169.  
  170. /* operator - */
  171. inline complex operator - ( const complex& lhs, const complex& rhs )
  172. {
  173.   return  complex ( lhs.re - rhs.re, lhs.im - rhs.im );
  174. }
  175. inline complex operator - ( FLT_TYPE lhs, const complex& rhs )
  176. {
  177.   return  complex ( lhs - rhs.re, - rhs.im );
  178. }
  179. inline complex operator - ( const complex& lhs, FLT_TYPE rhs )
  180. {
  181.   return  complex ( lhs.re - rhs, lhs.im );
  182. }
  183.  
  184. /* operator * */
  185. inline complex operator * ( const complex& lhs, const complex& rhs )
  186. {
  187.   return  complex ( lhs.re * rhs.re - lhs.im * rhs.im, 
  188.            lhs.re * rhs.im + lhs.im*rhs.re );
  189. }
  190. inline complex operator * ( FLT_TYPE lhs, const complex& rhs )
  191. {
  192.   return  complex ( lhs * rhs.re, lhs * rhs.im );
  193. }
  194. inline complex operator * ( const complex& lhs, FLT_TYPE rhs )
  195. {
  196.   return  complex ( lhs.re * rhs, lhs.im*rhs );
  197. }
  198.  
  199. /* operator / */
  200.  
  201. inline complex operator / ( const complex& lhs, FLT_TYPE rhs )
  202. {
  203.   return complex( lhs.re / rhs, lhs.im /rhs );
  204. }
  205.  
  206. /* unary operators */
  207. inline complex complex::operator - ( ) const
  208. {
  209.   return  complex ( -re, -im );
  210. }
  211.  
  212. inline int complex::operator ! ( )  const
  213. {
  214.   return  ( re == 0 ) && ( im == 0 ) ;
  215. }    
  216.  
  217.  
  218. inline complex & complex::operator = ( const complex& z)
  219. {
  220.   re = z.re;
  221.   im = z.im;
  222.   return  *this;
  223. }    
  224.  
  225. inline complex & complex::operator = ( FLT_TYPE x)
  226. {
  227.   re = x;
  228.   im = 0;
  229.   return  *this;
  230. }    
  231.  
  232. /* operator *= */
  233. inline complex & complex::operator *= ( const complex& z)
  234. {
  235.   *this = *this * z;
  236.   return  *this;
  237. }
  238. inline complex & complex::operator *= ( FLT_TYPE x )
  239. {
  240.   re *= x;
  241.   im *= x;
  242.   return  *this;
  243. }
  244.  
  245. inline complex & complex::operator /= ( const complex& z)
  246. {
  247.   *this = *this / z;
  248.   return  *this;
  249. }    
  250. inline complex & complex::operator /= ( FLT_TYPE x)
  251. {
  252.   re /= x;
  253.   im /= x;
  254.   return  *this;
  255. }    
  256.  
  257. inline complex & complex::operator += ( const complex& z)
  258. {
  259.   re += z.re;
  260.   im += z.im;
  261.   return  *this;
  262. }
  263. inline complex & complex::operator += ( FLT_TYPE x)
  264. {
  265.   re += x;
  266.   return  *this;
  267. }
  268.  
  269. inline complex & complex::operator -= ( const complex& z)
  270. {
  271.   re -= z.re;
  272.   im -= z.im;
  273.   return  *this;
  274. }
  275.  
  276. inline complex & complex::operator -= ( FLT_TYPE x)
  277. {
  278.   re -= x;
  279.   return  *this;
  280. }    
  281.  
  282. /* operator && */
  283. inline int operator && ( const complex & lhs, const complex & rhs )
  284. {
  285.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) &&
  286.     (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  287. }    
  288. inline int operator && ( FLT_TYPE lhs, const complex & rhs )
  289. {
  290.   return  ( lhs != 0 ) && (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  291. }    
  292. inline int operator && ( const complex & lhs, FLT_TYPE rhs )
  293. {
  294.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) && ( rhs != 0 ) ;
  295. }    
  296.  
  297. /* operator || */
  298. inline int operator || ( const complex & lhs, const complex & rhs )
  299. {
  300.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) ||
  301.     (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  302. }
  303. inline int operator || ( FLT_TYPE lhs, const complex & rhs )
  304. {
  305.   return  ( lhs != 0 ) || (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  306. }
  307. inline int operator || ( const complex & lhs, FLT_TYPE rhs )
  308. {
  309.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) || ( rhs != 0 );
  310. }    
  311.  
  312. /* operator != */
  313. inline  int operator != ( const complex& lhs, const complex& rhs )
  314. {
  315.   return  ( lhs.re != rhs.re ) || ( lhs.im != rhs.im );
  316. }
  317. inline  int operator != ( FLT_TYPE lhs, const complex& rhs )
  318. {
  319.   return  ( lhs != rhs.re ) || ( 0 != rhs.im );
  320. }
  321. inline  int operator != ( const complex& lhs, FLT_TYPE rhs )
  322. {
  323.   return  ( lhs.re != rhs ) || ( lhs.im != 0 );
  324. }
  325.  
  326. /* operator == */
  327. inline  int operator == ( const complex& lhs, const complex& rhs )
  328. {
  329.   return  ( lhs.re == rhs.re ) && ( lhs.im == rhs.im );
  330. }
  331. inline  int operator == ( FLT_TYPE lhs, const complex& rhs )
  332. {
  333.   return  ( lhs == rhs.re ) && ( 0 == rhs.im );
  334. }
  335. inline  int operator == ( const complex& lhs, FLT_TYPE rhs )
  336. {
  337.   return  ( lhs.re == rhs ) && ( lhs.im == 0 );
  338. }
  339.  
  340. inline FLT_TYPE arg( const complex& z)
  341. {
  342.   return atan2(z.im,z.re);
  343. }
  344.  
  345. inline complex atanh( const complex& z) 
  346. {
  347.   complex iz(-z.im, z.re);
  348.   complex ix = atan(iz);
  349.   return complex ( ix.im, -ix.re);
  350. }
  351.  
  352. inline complex asinh( const complex& z)
  353. {
  354.   complex iz(-z.im, z.re);
  355.   complex ix = asin(iz);
  356.   return complex ( ix.im, -ix.re);
  357. }
  358.  
  359. inline FLT_TYPE norm( const complex& z)
  360. {
  361.   return z.re*z.re + z.im*z.im;
  362. }
  363.  
  364. inline FLT_TYPE modulus( const complex& z)
  365. {
  366.   return abs(z);
  367. }
  368.  
  369. inline complex conj( const complex& z)
  370. {
  371.   return complex( z.re, -z.im );
  372. }
  373.  
  374. inline complex cos( const complex& z )
  375. {
  376.   return complex( cos(z.re) * cosh( z.im ),  -( sin( z.re) * sinh(z.im)));
  377. }
  378.  
  379. inline complex cosh( const complex& z )
  380. {
  381.   return complex ( cosh(z.re) * cos(z.im), sinh(z.re) * sin(z.im) );
  382. }
  383.  
  384. inline complex sin( const complex& z )
  385. {
  386.   return complex ( sin(z.re) * cosh(z.im), cos(z.re) * sinh(z.im) );
  387. }
  388.  
  389. inline complex sinh( const complex& z )
  390. {
  391.   return complex ( sinh(z.re) * cos(z.im), cosh(z.re) * sin(z.im) );
  392. }
  393.  
  394. inline complex tan( const complex& z )
  395. {
  396.   FLT_TYPE x = 2*z.re;
  397.   FLT_TYPE y = 2*z.im;
  398.   FLT_TYPE t = 1.0/(cos(x) +cosh(y));
  399.   
  400.   return complex( t*sin(x), t*sinh(y) );
  401. }
  402.  
  403. inline complex tanh( const complex& z )
  404. {
  405.   FLT_TYPE x = 2*z.re;
  406.   FLT_TYPE y = 2*z.im;
  407.   FLT_TYPE t = 1.0/(cosh(x) +cos(y));
  408.   
  409.   return complex( t*sinh(x), t*sin(y) );
  410. }    
  411.  
  412. inline complex exp( const complex& z )
  413. {
  414.   FLT_TYPE x = exp(z.re);
  415.   return complex( x*cos(z.im), x*sin(z.im) );
  416. }    
  417.  
  418. inline complex log( const complex& z )
  419. {
  420.   return complex( log( abs(z) ), arg( z ) );
  421. }    
  422.  
  423. inline complex log10( const complex& z )
  424. {
  425.   return complex( 0.2171472409516259*log( norm(z) ), arg( z ) );
  426. }    
  427.  
  428. inline complex polar ( FLT_TYPE r, FLT_TYPE theta )
  429. {
  430.   return complex ( r * cos(theta), r * sin(theta));
  431. }
  432.  
  433. #endif
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.